package com.cuitrip.component.seek;


import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.SeekBar;
/**
 * Created by ak
 */
public class VerticalSeekBar extends SeekBar {

    /*private PopupWindow mPopupWindow;
    private View mPopupView;
    private TextView mEqPopText;*/
    private int[] mPosition;
    private static final int POPWINDOW_DEFAULT_OFFSET = 200;
    private int originalProgress = 0; //为解决viewpager和seekbar的冲突, 记录之前一次seekbar的位置, 如果当前手指接触的位置大于之前位置偏移12个单位以上, 就当做什么都没发生
    private boolean inTouch = false;
    private static final int PROGRESS_OFFSET = 12;


    public VerticalSeekBar(Context context) {
        super(context);
    }

    public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initPopupWindows(context);
    }

    public VerticalSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPopupWindows(context);
    }

    private void initPopupWindows(Context context) {
//        mPopupView = LayoutInflater.from(context).inflate(R.layout.equalizer_popup_window, null);
//        mEqPopText = (TextView) mPopupView.findViewById(R.id.text_equalizer_popup);
//        mPopupWindow = new PopupWindow(mPopupView, mPopupView.getWidth(), mPopupView.getHeight(), false);
        mPosition = new int[2];
        inTouch = false;
    }

    private int getViewWidth(View v) {
        int w = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        int h = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        v.measure(w, h);
        return v.getMeasuredWidth();
    }

    private int getViewHeight(View v) {
        int w = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        int h = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        v.measure(w, h);
        return v.getMeasuredHeight();
    }

    public void hideSeekDialog() {
        /*if (mPopupWindow != null && mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
        }*/
    }

    public void showSeekDialog(View view, int progress) {
        int db = (int)(progress / 4.16 - 12);
//        mEqPopText.setText((db > 0 ? "+" : "") + db + "db");

//        if (mPopupWindow != null) {
            view.getLocationOnScreen(mPosition);
//            mPopupWindow.showAsDropDown(view, 0, 0);
            int thumb_y = mPosition[1] + (int) ((100 - progress)
                    * (1.0f * (view.getHeight() -view.getPaddingRight()-view.getPaddingLeft())) / 100);
            int y = thumb_y + progress - POPWINDOW_DEFAULT_OFFSET;
            // 更新popup窗口的位置
//            mPopupWindow.update(mPosition[0], y, getViewWidth(mPopupView), getViewHeight(mPopupView));
//        }
    }

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(h, w, oldh, oldw);
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
        setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
    }

    protected void onDraw(Canvas c) {
        c.rotate(-90);
        c.translate(-getHeight(),0);

        super.onDraw(c);
    }

    public void setOriginalProgress(int progress) {
        originalProgress = progress;
    }

    public int calcProgress(float y) {
        int i =  getMax() - (int)(getMax() * (y - getPaddingRight())
                / (getHeight() - getPaddingLeft() - getPaddingRight()));
        if (i > 100) {
            return 100;
        } else if (i < 0) {
            return 0;
        }
        return i;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!isEnabled()) {
            return false;
        }
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_UP:
                int i = calcProgress(event.getY());
                if ((i > (originalProgress + PROGRESS_OFFSET))
                        || (i < (originalProgress - PROGRESS_OFFSET))) {
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        inTouch = false;
                        hideSeekDialog();
                    }
                    if (!inTouch)
                        return true;
                   else
                        originalProgress = i;
                } else {
                    originalProgress = i;
                }
                inTouch = true;
                setProgress(i);
               // Log.d("progress:" + getProgress());
                onSizeChanged(getWidth(),getHeight(), 0, 0);
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    inTouch = false;
                    hideSeekDialog();
                }
                break;
            case MotionEvent.ACTION_CANCEL:
                inTouch = false;
                hideSeekDialog();
                break;
        }
        return true;
    }

    public synchronized void setProgressAndThumb(int progress) {
        setProgress(progress);
        onSizeChanged(getWidth(), getHeight() , 0, 0);
    }
}
